****************************************************************************
** Stata code to construct figures and tables in main text and supplement **
****************************************************************************

clear all
set more off

set scheme burd 
global stataout [specify local folder for figures and tables]


******************************************************
** Main figures and tables (in order of appearance) **
******************************************************

preserve 
use "CAFC_ct_updated.dta", clear
sum gap_all 
global gap_mean = r(mean)
restore

** Subroutines **
capture program drop get_coef
program define get_coef
	quietly {  
	 `1' , absorb($fe)  
	 scalar nobs = e(N) 
	 capture scalar rsq = e(r2_a)
	 capture scalar rsq_p = e(r2_p)
	 capture scalar rsq_within = e(r2_a_within) 
	 capture scalar chi2 = e(chi2)
	 unique gvkey if e(sample) == 1
	 scalar cmp = `r(unique)'
	 lincom CAFC_post *$gap_mean 
	 local est = r(estimate)
	 local change = (exp(`est')-1)*100
	 scalar perct = `change'
	 matrix coef = e(b) 
	}   
end 
 
capture program drop reg_out 
program define reg_out 
cd "$stataout"
outreg2 using "`1'", word  nor2 noobs  /// 
      addtext("Technology-Year-Industry", "Yes", "Circuit FE", "Yes") ///
	addstat( "Companies", cmp, "Adjusted R-squared", rsq, ///
	"Within R-squared", rsq_within ,   "Economic effect", perct) ///
	 sortvar(_b_CAFC_post  _b_emp_ln _b_revt_per_inh _b_ppe_per_inh ///
	 _b_nclaims _b_novelty_res_inh  _b_nciting_inh _b_pcs_inh ///
	  _b_cons) bdec(3) tdec(3)  ctitle("`2'")
matrix drop _all 
scalar drop _all 
end 

capture program drop reg_out_count
program define reg_out_count 
cd "$stataout"
outreg2 using "`1'", word  nor2 noobs  /// 
      addtext("Technology-Year-Industry", "Yes", "Circuit FE", "Yes") ///
	addstat( "Companies", cmp, "Chi-squared", chi2, ///
	  "Economic effect", perct) ///
	 sortvar(_b_CAFC  _b_emp_ln _b_revt_per_inh _b_ppe_per_inh ///
	 _b_nclaims _b_novelty_res_inh  _b_nciting_inh _b_pcs_inh ///
	  _b_cons) bdec(3) tdec(3)  ctitle("`2'")
matrix drop _all 
scalar drop _all 
end 

global sample1 if !mi(subcat) & !mi(circuit) 

capture program drop tse_boot  
program tse_boot, eclass  
	quietly {  
		use "CAFC_reg.dta", clear  
		preserve  
		bsample, strata(ct_post)    
		xi: ologit outcome patdef claims ass home_circuit appeal  ///
		ct1 ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year ///
		$sample1, robust

		foreach j in 1 2 4 5 6 7 8 9 10{
		replace ratepre_all = _b[ct`j'] if circuit == `j'
		quietly lincom ct`j' +   cafcct`j'   
		replace ratepost_all = `r(estimate)' if circuit == `j' 
		}

		replace ratepre_all = 0 if circuit == 3
		replace ratepost_all = _b[cafcct3] if circuit == 3
		replace ratepre_all = _b[ct5] if circuit == 11
		replace ratepost_all = _b[cafcct11] if circuit == 11
		
		gen gap_all = ratepost_all - ratepre_all 
		keep circuit gap_all 
		duplicates drop 
		tempfile temp
		save `temp', replace
		restore  

		use "Patreg.dta", clear
		preserve  
		bsample  , cluster(gvkey circuit)
		merge m:1 circuit using `temp', update replace 
		replace CAFC = gap_all * post 
		 `1',  absorb($fe) 
		restore 
	 }  
end  


** Figure 1. Federal Circuit: Change in commercial value and tech quality **
use "Patreg.dta", clear
gen cafc = 1 if year >= 1986
replace cafc = 0 if year < 1983
keep if complex == 1
collapse  (mean) xi_mean = xi_w cites_mean = cites_others_3yr , ///
   by(circuit cafc)
merge m:1 circuit using "CAFC_ct_updated.dta"

keep if cafc != .
bysort circuit (cafc): gen xi_chg = xi_mean - xi_mean[_n-1]
bysort circuit (cafc): gen cites_chg = cites_mean - cites_mean[_n-1]

keep if cafc == 1
binscatter  xi_chg cites_chg gap_all, ///
  xtitle("Pro-patentee shift in rulings (CAFC index)") ///
  ytitle("Change in commercial value/technological quality") ///
  legend(pos(6) lab(1 "Commercial value") lab(2 "Technological quality"))   
graph export "$stataout\CAFC_chg_bins.png", as(png) replace


** Figure 2. Federal Circuit and appeals ** 
use "CAFC_reg.dta", replace 
tab year, gen(year)

global sample !mi(subcat) & !mi(circuit)
xi: logit appeal claims ass patdef home_circuit ct1 ct2 ct4-ct10 ///
  cafcct1 cafcct2 cafcct4-cafcct11 hjt1-hjt5  year1-year14 year16-year33  ///
   if $sample, robust

coefplot , vertical keep(year* )  ///
  recast(rcap) ciopts(lwidth(medthick) recast(rcap)) yline(0) ///
   xlabel( 1 "1968"   6 "1973"  11 "1978" 16 "1984"  21 "1989" 26 "1994" ///
   31 "1999")  xline(14.5)
graph export "$stataout\appeal.png" , replace as(png)


** Table 1. Summary statistics **

*** A. Litigation ***
use "CAFC_reg.dta", clear 
gen post = cafcera 

xi: ologit outcome patdef claims ass home_circuit appeal  ///
  ct1 ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year ///
   $sample1, robust
keep if e(sample)

gen byte invalid = 0 if !mi(outcome)
replace invalid = 1 if outcome == 0 

gen byte valinv = 0 if !mi(outcome)
replace valinv = 1 if outcome == 1

gen byte valid = 0 if !mi(outcome)
replace valid = 1 if outcome == 2

iebaltab invalid valinv valid appeal home_circuit, ///
 grpvar(post) save("$stataout\CAFC_lit_sumtab.xlsx") ///
 grplabels(0 Pre-CAFC @ 1 Post-CAFC)  rowvarlabels format(%9.2f) replace

 
*** B. Patents ***
use "Patreg.dta" , clear
keep if year >= 1976 & year <= 1992  
 // limit sample to 7-year windows around years of major CAFC decisions, 1983-98
global ctrvar emp_ln ppe_per_inh revt_per_inh xrd_per_inh tobin ///
       nclaims_ln  nciting_inh   
	   
reghdfe xi_ln CAFC  $ctrvar  if manuf == 1 , noa
gen sample = e(sample)	

label var xi_w "Patent value"
label var nciting "Backward citations"
label var nclaims "Claims"
label var cites_others "Forward citations"
label var pat_clm_ct "Minimum word court for independent claims"
label var pat_wrd_min "Independent claim length"
label var general "Generality"
label var orig "Originality"
label var pcs "Citations to science"
label var novelty_res "Patent novelty"
label var novelty
keep if sample == 1

iebaltab xi_w cites_others pcs pat_clm_ct nclaims  nciting, ///
 grpvar(post) save("$stataout\CAFC_pat_sumtab.xlsx") ///
 grplabels(0 Pre-CAFC @ 1 Post-CAFC)  rowvarlabels format(%9.2f) replace

 
*** C. Companies ***
gen xrd_per = xrd1/emp_ipolated

keep gvkey year emp_ipolated ppe_per revt_per xrd_per tobin  post
duplicates drop  

iebaltab emp_ipolated ppe_per revt_per xrd_per tobin , ///
 grpvar(post) save("$stataout\CAFC_cmp_sumtab.xlsx") ///
 grplabels(0 Pre-CAFC @ 1 Post-CAFC)  rowvarlabels format(%9.2f) replace
 

 
** Figure 3. Federal Circuit: Court rulings ** 
use "CAFC_ct_updated.dta" , clear
gen circuit_lab = circuit 
tostring circuit_lab, replace
replace circuit_lab = circuit_lab + "th"
replace circuit_lab = "1st" if circuit_lab == "1th"
replace circuit_lab = "2nd" if circuit_lab == "2th"
replace circuit_lab = "3rd" if circuit_lab == "3th"

*** A. Change in protection ***
twoway (scatter gap_all ratepre_all, ///
   mlabel(circuit_lab)  ///
   msymbol(Sh) msize(vlarge) mlabposition(6) ) , ///
   xtitle("Pre-CAFC rulings in favor of patentees") ///
   ytitle("Pro-patentee shift in rulings (CAFC index)") 
graph export "$stataout\CAFC_chg.png" , replace as(png)

*** B. Before/after CAFC
twoway (scatter ratepost_all ratepre_all, ///
   mlabel(circuit_lab) msymbol(Sh) msize(vlarge) mlabposition(6) ) , ///
   xtitle("Pre-CAFC rulings in favor of patentees") ///
   ytitle("Post-CAFC rulings in favor of patentees")
graph export "$stataout\CAFC_prepost.png" ,as(png) replace 


** Figure 4. Federal Circuit and commercial value of patents **
use "Patreg.dta" , clear
merge m:1 circuit using "CAFC_ct_updated", nogen
global reps 1000
global seed 20220608

global fe year##nclass_ocl##sic2 circuit 
global ctrvar emp_ln ppe_per_inh revt_per_inh xrd_per_inh tobin ///
       nclaims_ln  nciting_inh   
	   
forvalues i = 1976/1992{
 gen post_`i' = cond(year == `i', 1, 0)
 gen CAFC_`i' = gap_all *  post_`i'
}
drop post*

xi: bootstrap, seed($seed) reps($reps): ///
reghdfe xi_ln CAFC_1976-CAFC_1981 CAFC_1983-CAFC_1992 $ctrvar, ///
    absorb($fe) cluster(circuit gvkey) 
coefplot , vertical keep(CAFC_* )  ///
  recast(rcap) ciopts(lwidth(medthick) recast(rcap)) xline(6.5) yline(0) ///
   xlabel( 1 "1976"   5 "1980"  9 "1985" 13 "1989"  16 "1992" )  
graph export "$stataout\cafc-falsi-patval-1982.png", as(png) replace 

xi: bootstrap, seed($seed) reps($reps): ///
ppmlhdfe cites_others_3yr CAFC_1976-CAFC_1981 CAFC_1983-CAFC_1992 $ctrvar, ///
    absorb($fe) cluster(circuit gvkey) 
coefplot , vertical keep(CAFC_* )  ///
  recast(rcap) ciopts(lwidth(medthick) recast(rcap)) xline(6.5) yline(0) ///
   xlabel( 1 "1976"   5 "1980"  9 "1985" 13 "1989"  16 "1992" )  
graph export "$stataout\cafc-falsi-patval-1982.png", as(png) replace 


** Table 2. Federal Circuit and patents: Commercial value .. **
** Refer to table2&4.do **


** Table 3. Federal Circuit and commercial value: Robustness tests **
** Refer to table3.do **


** Table 4. Federal Circuit and commercial value: Alternative explanations **
** Refer to table2&4.do **


***************************************************************
** Supplementary figures and tables (in order of appearance) **
***************************************************************

** Table A1: CAFC rulings **
use "CAFC_ct_updated.dta", clear
keep circuit ratepost_all sepost_all ppost_all 

tempfile a
savesome circuit ratepost_all using `a', replace
tempfile b 
savesome circuit sepost_all using `b', replace
tempfile c
savesome circuit ppost_all using `c', replace

use `a', clear
rename circuit var
tostring var, replace
replace var = "outcome:" + "ct" + var + "post" + "_coef"
replace ratepost_all = round(ratepost_all, 0.001)  
format %8.3f ratepost_all
tostring ratepost_all, gen(CAFC) force usedisplayformat
drop ratepost_all
save `a', replace

use `b', clear
rename circuit var
tostring var, replace
replace var = "outcome:" +  "ct" + var + "post" + "_stderr"
replace sepost_all = round(sepost_all, 0.001)  
format %8.3f sepost_all
tostring sepost_all, gen(CAFC) force usedisplayformat
drop sepost_all
replace CAFC = "(" + CAFC + ")"
save `b', replace 

use `c', clear
rename circuit var
tostring var, replace 
replace var = "outcome:" + "ct" + var + "post" + "_pval"
replace ppost_all = round(ppost_all, 0.01)  
format %8.3f ppost_all
tostring ppost_all, gen(CAFC) force usedisplayformat
drop ppost_all

append using `a'
append using `b'

append using "CAFC_regsave.dta"
 
drop if strpos(var, "Iyear")
drop if strpos(var, "hjt")

replace var = subinstr(var, "outcome:", "", .)

forvalues i = 1/11{
 foreach j in coef stderr pval{
 replace var = "ct`i'change_`j'"  if var == "cafcct`i'_`j'" 
}
}

expand 3 if strpos(var, "ct") == 0
bysort var: gen seq = _n
replace var = var + "post" if seq == 2
replace var = var + "change" if seq == 3

gen index = "pre" if strpos(var, "post") == 0
replace index = "post" if strpos(var, "post")
replace index = "change" if strpos(var, "change")
replace var = subinstr(var, "post", "",.)
replace var = subinstr(var, "change", "",.)

drop seq
reshape wide CAFC, i(var) j(index) string

gen var_label = "Coefficient" if strpos(var, "_coef")
replace var_label = "Std.Err" if strpos(var, "_stderr")
replace var_label = "pval" if strpos(var, "_pval")
replace var = subinstr(var, "_coef", "",.)
replace var = subinstr(var, "_stderr", "",.)
replace var = subinstr(var, "_pval", "",.)
replace var = subinstr(var, "ct", "Circuit",.)

foreach i in pre post change{
gen p_`i' = CAFC`i' if var_label == "pval" 
destring p_`i' , replace
bysort var: egen mean_p`i' = mean(p_`i')
gen star_`i' = "***" if mean_p`i' == 0
replace star_`i' = "**" if mean_p`i' > 0 & mean_p`i' <= 0.05
replace star_`i' = "*" if mean_p`i' > 0.05 & mean_p`i' <= 0.1
replace CAFC`i' = CAFC`i' + star_`i' if var_label == "Coefficient"
}

sort var var_label
drop p_pre-star_change
drop if var_label == "pval"

rename CAFCpre pre_CAFC
rename CAFCpost post_CAFC
rename CAFCchange change_CAFC
order var pre_CAFC change_CAFC post_CAFC

gen ct = subinstr(var, "Circuit", "", .)
replace ct = "" if strpos(var, "Circuit") == 0
destring ct, replace
replace ct = 12 if var == "ass"
replace ct = 13 if var == "claims"
replace ct = 14 if var == "home_circuit"
replace ct = 15 if var == "appeal"
replace ct = 16 if var == "patdef"
replace ct = 17 if var == "/cut1"
replace ct = 18 if var == "/cut2"

bysort var (var_label): gen seq = _n
sort ct  var_label var
replace var = "" if seq == 2

gen ct_lab = ""
replace ct_lab = subinstr(var, "Circuit", "", .)
replace ct_lab = ct_lab + "th" if strpos(var, "Circuit")
replace ct_lab = "1st" if ct_lab == "1th"
replace ct_lab = "2nd" if ct_lab == "2th"
replace ct_lab = "3rd" if ct_lab == "3th"
replace ct_lab = ct_lab + " Circuit" if strpos(var, "Circuit")

replace ct_lab = "Business patentee" if ct_lab == "ass"
replace ct_lab = "Number of claims" if ct_lab == "claims"
replace ct_lab = "Home circuit" if ct_lab == "home_circuit"
replace ct_lab = "Appeal" if ct_lab == "appeal"
replace ct_lab = "Patentee as the defendant" if ct_lab == "patdef"

order ct_lab pre_CAFC change_CAFC post_CAFC
replace pre_CAFC = "0" if ct_lab == "3rd Circuit"

replace pre_CAFC = "1.197" if ct_lab == "11th Circuit"
replace change_CAFC = "-0.1" if ct_lab == "11th Circuit"

*replace post_CAFC = "" if strpos(var, "Circuit") == 0
drop var-seq
export excel using "\stataout\CAFC_pub.xlsx", firstrow(varlabels) replace 



** Figure A1. CAFC: Pro-patentee Change in Rulings **

shp2dta using s_11au16, database(usdb) coordinates(uscoord) genid(id)
use "usdb.dta", clear
rename STATE state_code
drop if LON == 0
save "usdb.dta", replace 

use "usdb.dta", clear
merge 1:1 state_code using "fedapp.dta"
drop if _merge == 1
drop _merge 
merge m:1 circuit using "CAFC_ct_updated.dta", nogen
gen circuit_lab = circuit 
tostring circuit_lab, replace
replace circuit_lab = circuit_lab + "th"
replace circuit_lab = "1st" if circuit_lab == "1th"
replace circuit_lab = "2nd" if circuit_lab == "2th"
replace circuit_lab = "3rd" if circuit_lab == "3th"
format gap_all %8.2f
drop if NAME == "Alaska" | NAME  =="Hawaii" | circuit == 12 | state_code == "DC"
save "circuit_map.dta", replace

spmap gap_all using "uscoord.dta" ,  ///
 id(id) fcolor(Oranges) label(data("circuit_map.dta") ///
 label(circuit_lab) xcoord(LON) ycoord(LAT)) 
graph export "$stataout\CAFCchange_map.png", as(png) replace 


** Figure A2. CAFC Index: Robustness **
use "CAFC_reg.dta", replace 

gen circuit_lab = circuit 
tostring circuit_lab, replace
replace circuit_lab = circuit_lab + "th"
replace circuit_lab = "1st" if circuit_lab == "1th"
replace circuit_lab = "2nd" if circuit_lab == "2th"
replace circuit_lab = "3rd" if circuit_lab == "3th"

foreach i in 1 2 3 4{
gen ratepre_all_`i' = . 
gen ratepost_all_`i' = . 
}

label var ratepre_all_1 "Preferred estimate"
label var ratepost_all_1 "Preferred estimate"
label var ratepre_all_2  "Patent characteristics" 
label var ratepost_all_2 "Patent characteristics" 
label var ratepre_all_3 "Litigation characteristics"
label var ratepost_all_3 "Litigation characteristics"
label var ratepre_all_4 "Year fixed effects"
label var ratepost_all_4 "Year fixed effects"

xi: ologit outcome patdef claims ass home_circuit appeal  ///
  ct1 ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year ///
   $sample1, robust

foreach j in 1 2 4 5 6 7 8 9 10{
  replace ratepre_all_1 = _b[ct`j'] if circuit == `j'
  quietly lincom ct`j' +   cafcct`j'   
  replace ratepost_all_1 = `r(estimate)' if circuit == `j' 
}

replace ratepre_all_1 = 0 if circuit == 3
replace ratepost_all_1 = _b[cafcct3] if circuit == 3
replace ratepre_all_1 = _b[ct5] if circuit == 11
replace ratepost_all_1 = _b[cafcct11] if circuit == 11

xi: ologit outcome patdef claims ass ct1 ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5  $sample1, robust

foreach j in 1 2 4 5 6 7 8 9 10{
  replace ratepre_all_2 = _b[ct`j'] if circuit == `j'
  quietly lincom ct`j' +   cafcct`j'   
  replace ratepost_all_2 = `r(estimate)' if circuit == `j' 
}

replace ratepre_all_2 = 0 if circuit == 3
replace ratepost_all_2 = _b[cafcct3] if circuit == 3
replace ratepre_all_2 = _b[ct5] if circuit == 11
replace ratepost_all_2 = _b[cafcct11] if circuit == 11


xi: ologit outcome ct1 ct2 ct4-ct10 cafcct1-cafcct11 home_circuit appeal  $sample1, robust

foreach j in 1 2 4 5 6 7 8 9 10{
  replace ratepre_all_3 = _b[ct`j'] if circuit == `j'
  quietly lincom ct`j' +   cafcct`j'   
  replace ratepost_all_3 = `r(estimate)' if circuit == `j' 
}

replace ratepre_all_3 = 0 if circuit == 3
replace ratepost_all_3 = _b[cafcct3] if circuit == 3
replace ratepre_all_3 = _b[ct5] if circuit == 11
replace ratepost_all_3 = _b[cafcct11] if circuit == 11

xi: ologit outcome ct1 ct2 ct4-ct10 cafcct1-cafcct11 i.year  $sample1, robust

foreach j in 1 2 4 5 6 7 8 9 10{
  replace ratepre_all_4 = _b[ct`j'] if circuit == `j'
  quietly lincom ct`j' +   cafcct`j'   
  replace ratepost_all_4 = `r(estimate)' if circuit == `j' 
}

replace ratepre_all_4 = 0 if circuit == 3
replace ratepost_all_4 = _b[cafcct3] if circuit == 3
replace ratepre_all_4 = _b[ct5] if circuit == 11
replace ratepost_all_4 = _b[cafcct11] if circuit == 11

foreach i in 1 2 3 4{
gen gap`i' = ratepost_all_`i' - ratepre_all_`i'
}

keep circuit ratepre* gap*  circuit_lab 
duplicates drop 

twoway (scatter gap1 ratepre_all_1, mlabel(circuit_lab) ///
    mlabsize(small) mlabco(black) mlabposition(6)   ///
    msymbol(Sh) msize(vlarge) ) ///
  (scatter gap2 ratepre_all_2,  ///
    msymbol(Dh) msize(large) mlabposition(0)) ///
  (scatter gap3 ratepre_all_3,   ///
    msymbol(Oh) msize(large) mlabposition(0)) ///
  (scatter gap4 ratepre_all_4, msymbol(Th) msize(large) ) , ///
   xtitle("Pre-CAFC patent protection") ///
   ytitle("Pro-patentee shift") ///
    legend( lab(1 "All controls(Preferred estimate)" ) ///
    lab(2  "Patent characteristics") lab(3 "Litigation characteristics") ///
	lab(4 " Year fixed effects") position(6)) 
graph export "$stataout\CAFC_prepost_fourindex.png" , as(png) replace


** Table A2. CAFC and technological quality **
** Refer to TableA2.do **
